﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UApiService;

namespace TouchScreen.Controllers
{
    public class News1Controller : Controller
    {
        //
        // GET: /News/

        public ActionResult List()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Edit()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult TableNews(Guid? ID, string Title, string Subtitle, Guid ColumnID, HttpPostedFileBase Cover, HttpPostedFileBase Video, string ContentTxt, string HTML)
        {
            Guid NewID = ID == null ? Guid.NewGuid() : ID.Value;

            Dictionary<string, object> data = new Dictionary<string, object>()
            {
                {"ID", NewID},
                {"Title", Title},
                {"Subtitle", Subtitle},
                {"ColumnID", ColumnID},
                {"Content", ContentTxt},
                {"HTML", HTML}
            };

            if (Cover != null)
            {
                string fileFormat1 = ".jpg.jpeg.png";

                string Suffix1 = Cover.FileName.Substring(Cover.FileName.LastIndexOf('.'));

                if (fileFormat1.IndexOf(Suffix1) > -1)
                {
                    string SaveURL1 = "/Upload/Images/" + NewID + Suffix1;
                    string CoverURL = Server.MapPath(SaveURL1);
                    Cover.SaveAs(CoverURL);
                    data.Add("CoverURL", SaveURL1);
                }

            }
            if (Video != null)
            {
                string fileFormat2 = ".mp4.mp3";

                string Suffix2 = Video.FileName.Substring(Video.FileName.LastIndexOf('.'));

                if (fileFormat2.IndexOf(Suffix2) > -1)
                {
                    string SaveURL2 = "/Upload/Video/" + NewID + Suffix2;
                    string VideoURL = Server.MapPath(SaveURL2);
                    Video.SaveAs(VideoURL);
                    data.Add("VideoURL", SaveURL2);
                }
            }

            BizCenter biz = new BizCenter();

            if (ID == null)
            {
                data.Add("CreateTime", DateTime.Now);
                data.Add("IsDelete", 0);
                CommonApiPackage api = biz.ApiGet(UserInfoEx.GetUserInfoEx(), "Corey_TableNews", data, true, true);
                return Redirect("Add?S=1");
            }
            else
            {
                CommonApiPackage api = biz.ApiUpdate("Corey_TableNews", data);
                return Redirect("List");
            }

        }

    }
}
